package com.ahdy.service.impl;

import com.ahdy.entity.DailyMedicationTask;
import com.ahdy.mapper.DailyMedicationTaskMapper;
import com.ahdy.service.DailyMedicationTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 每日用药任务服务实现类
 */
@Service
public class DailyMedicationTaskServiceImpl implements DailyMedicationTaskService {

    @Autowired
    private DailyMedicationTaskMapper dailyMedicationTaskMapper;

    @Override
    @Transactional
    public Integer addDailyMedicationTask(DailyMedicationTask task) {
        dailyMedicationTaskMapper.insert(task);
        return task.getId();
    }

    @Override
    @Transactional
    public int updateTaskStatus(Integer id, DailyMedicationTask.TaskStatus status) {
        return dailyMedicationTaskMapper.updateStatus(id, status);
    }

    @Override
    @Transactional
    public int deleteDailyMedicationTask(Integer id) {
        return dailyMedicationTaskMapper.deleteById(id);
    }

    @Override
    public DailyMedicationTask getDailyMedicationTaskById(Integer id) {
        return dailyMedicationTaskMapper.findById(id);
    }

    @Override
    public List<DailyMedicationTask> getDailyMedicationTasksByUserIdAndDate(Integer userId, LocalDate taskDate) {
        return dailyMedicationTaskMapper.findByUserIdAndDate(userId, taskDate);
    }

    @Override
    public List<DailyMedicationTask> getDailyMedicationTasksByUserIdAndDateRange(Integer userId, LocalDate startDate,
            LocalDate endDate) {
        return dailyMedicationTaskMapper.findByUserIdAndDateRange(userId, startDate, endDate);
    }

    @Override
    public List<DailyMedicationTask> getUncompletedTasksByDate(LocalDate taskDate) {
        return dailyMedicationTaskMapper.findUncompletedByDate(taskDate);
    }

    @Override
    @Transactional
    public int deleteDailyMedicationTasksByUserId(Integer userId) {
        return dailyMedicationTaskMapper.deleteByUserId(userId);
    }

    @Override
    @Transactional
    public int deleteDailyMedicationTasksByPlanId(Integer planId) {
        return dailyMedicationTaskMapper.deleteByPlanId(planId);
    }

    @Override
    @Transactional
    public int batchAddDailyMedicationTasks(List<DailyMedicationTask> tasks) {
        return dailyMedicationTaskMapper.batchInsert(tasks);
    }

    @Override
    public Map<String, Object> getTodayTaskStatistics(Integer userId) {
        return dailyMedicationTaskMapper.getTodayTaskStatistics(userId);
    }

    @Override
    public List<DailyMedicationTask> getTasksNeedingReminder(LocalDate date, String currentTime) {
        return dailyMedicationTaskMapper.findTasksNeedingReminder(date, currentTime);
    }

    @Override
    public List<Map<String, Object>> getDelayedTasksForNotification(LocalDate date, Integer delayMinutes) {
        return dailyMedicationTaskMapper.findDelayedTasksForNotification(date, delayMinutes);
    }
}